import { capitalize } from '@/helpers/utils.ts';
import { UseDispatch, useDispatch } from 'react-redux';
import { Action } from '@reduxjs/toolkit';
import { ReduxDispatch } from '@/mySliceCreator.ts';

type DispatchI<Name extends string, T, Actions extends string> = {
  [K in `use${Capitalize<Name>}Dispatch`]: UseDispatch<
    ReduxDispatch<T, Action<`${Name}/${Actions}`>>
  >;
};
export const useGenerateDispatch = <
  S,
  SliceName extends string,
  ValidActionsNames extends string,
>(
  name: SliceName,
): DispatchI<SliceName, S, ValidActionsNames> => {
  const key = `use${capitalize(name)}Dispatch`;

  const getDispatch = useDispatch.withTypes<ReduxDispatch<S>>();

  return {
    [key]: getDispatch,
  } as DispatchI<SliceName, S, ValidActionsNames>;
};
